package com.mzj.saas.mservice.contract.controller;

import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.contract.service.ContractAppService;
import com.mzj.saas.mservice.contract.vo.ContractApiData;
import com.mzj.saas.mservice.contract.vo.ContractDetailVO;
import com.mzj.saas.mservice.contract.vo.ContractListVO;
import com.mzj.saas.mservice.contract.vo.ContractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 合同app接口
 */
@Controller
@RequestMapping("/contractApp")
public class ContractAppController {
    @Autowired
    private ContractAppService contractAppService;

    /**
     * 合同列表，实现合同列表的展示功能
     *
     * @param accessToken 权限鉴定
     * @param status 合同状态：0初稿 1待签约、2已签约待确认、3已确认待交租、4已交租待备案、5已备案履约中、6已到期待续约 7已续约
     * @param pageNumber 当前页
     * @param pageSize 每页条数
     * @return
     */
    @GetMapping("/contractList")
    @ResponseBody
    public ResultBean<ContractApiData<ContractListVO>> contractList(@RequestHeader String accessToken,
                                                                    @RequestParam(value = "status", required = true) Integer status,
                                                                    @RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize) {
        return contractAppService.contractList(accessToken, status, pageNumber, pageSize);
    }

    /**
     * 合同模板列表
     *
     * @return
     */
    @GetMapping("/contractTemplate")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> contractTemplate(@RequestHeader String accessToken) {
        return contractAppService.contractTemplate(accessToken);
    }

    /**
     * 合同模板字段-生成合同
     *
     * @param contractTemplateNumber 合同模板序号
     * @return
     */
    @GetMapping("/contractTemplateField")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> contractTemplateField(@RequestHeader String accessToken,
                                                                       @RequestParam(value = "contractTemplateNumber") String contractTemplateNumber,
                                                                       @RequestParam(value = "houseId") String houseId,
                                                                       @RequestParam(value = "matchUserId", required = false) String matchUserId) {
        return contractAppService.contractTemplateField(accessToken, contractTemplateNumber, houseId, matchUserId);
    }

    /**
     * 合同模板附件
     *
     * @param contractTemplateNumber 合同模板序号
     * @return
     */
    @GetMapping("/contractTemplateAttachment")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> contractTemplateAttachment(
            @RequestParam(value = "contractTemplateNumber") String contractTemplateNumber) {
        return contractAppService.contractTemplateAttachment(contractTemplateNumber);
    }

    /**
     * 合同预览(保存草稿合同)
     *
     * @param accessToken
     * @param vo
     * @param customField
     * @param contractAttachment
     * @return
     */
    @PostMapping("/saveContract")
    @ResponseBody
    public ResultBean<Object> saveContract(@RequestHeader String accessToken, ContractVO vo,
                                           @RequestParam(value = "customField", required = false) String customField,
                                           @RequestParam(value = "contractAttachment", required = false) String contractAttachment) {
        return contractAppService.saveContract(accessToken, vo, customField, contractAttachment);
    }

    /**
     * 甲方上传签字图片，实现了手写签名图片的存储
     *
     * @param contractNubmer 合同编号
     * @param file 签字图片
     * @return
     */
    @PostMapping("/lessorUploadSign")
    @ResponseBody
    public ResultBean<Object> lessorUploadSign(@RequestParam(value = "contractNubmer") String contractNubmer,
                                               @RequestParam(value = "file") MultipartFile file) {
        return contractAppService.lessorUploadSign(contractNubmer, file);
    }

    /**
     * 乙方上传签字图片，，实现了手写签名图片的存储
     *
     * @param contractNubmer 合同编号
     * @param file 签字图片
     * @return
     */
    @PostMapping("/leaseeUploadSign")
    @ResponseBody
    public ResultBean<Object> leaseeUploadSign(@RequestParam(value = "contractNubmer") String contractNubmer,
                                               @RequestParam(value = "file") MultipartFile file) {
        return contractAppService.leaseeUploadSign(contractNubmer, file);
    }

    /**
     * 生成合同按钮
     *
     * @param contractNubmer 合同编号
     * @return
     */
    @GetMapping("/createButton")
    @ResponseBody
    public ResultBean<Object> createButton(@RequestParam(value = "contractNubmer") String contractNubmer,
                                           @RequestParam(value = "signingFileUrl") String signingFileUrl) {
        return contractAppService.createButton(contractNubmer, signingFileUrl);
    }

    /**
     * 查看签字图片
     *
     * @param contractNubmer 合同编号
     * @return
     */
    @GetMapping("/findSignPic")
    @ResponseBody
    public ResultBean<Map<String, Object>> findSignPic(@RequestParam(value = "contractNubmer") String contractNubmer) {
        return contractAppService.findSignPic(contractNubmer);
    }

    /**
     * 查看合同模板名称
     *
     * @param contractNubmer
     * @return
     */
    @GetMapping("/findTemplate")
    @ResponseBody
    public ResultBean<Object> findTemplate(@RequestParam(value = "contractNubmer") String contractNubmer) {
        return contractAppService.findTemplate(contractNubmer);
    }

    /**
     * 查看合同字段
     *
     * @param contractNubmer
     * @return
     */
    @GetMapping("/findContractField")
    @ResponseBody
    public ResultBean<Object> findContractField(@RequestParam(value = "contractNubmer") String contractNubmer) {
        return contractAppService.findContractField(contractNubmer);
    }

    /**
     * 查看合同附件
     *
     * @param contractNubmer
     * @return
     */
    @GetMapping("/findContractAttachment")
    @ResponseBody
    public ResultBean<Object> findContractAttachment(@RequestParam(value = "contractNubmer") String contractNubmer) {
        return contractAppService.findContractAttachment(contractNubmer);
    }

    /**
     * 合同条款
     *
     * @param contractNubmer
     * @return
     */
    @GetMapping("/templateContent")
    @ResponseBody
    public ResultBean<String> templateContent(@RequestParam(value = "contractNubmer") String contractNubmer) {
        return contractAppService.templateContent(contractNubmer);
    }

    /**
     * 确认签约
     *
     * @param contractNubmer
     * @param contractFileUrl
     * @param contractTime
     * @return
     */
    @GetMapping("/signConfirm")
    @ResponseBody
    public ResultBean<Object> signConfirm(@RequestParam(value = "contractNubmer") String contractNubmer,
                                          @RequestParam(value = "contractFileUrl") String contractFileUrl,
                                          @RequestParam(value = "contractTime") String contractTime) {
        return contractAppService.signConfirm(contractNubmer, contractFileUrl, contractTime);
    }

    /**
     * 产权人确认按钮
     *
     * @param contractNubmer
     * @return
     */
    @GetMapping("/confirm")
    @ResponseBody
    public ResultBean<Object> confirm(@RequestHeader String accessToken,
                                      @RequestParam(value = "contractNubmer") String contractNubmer) {
        return contractAppService.confirm(accessToken, contractNubmer, null);
    }
    
    /**
     * 产权人确认按钮(包含下线合同文件)
     *
     * @param contractNubmer
     * @return
     */
    @GetMapping("/confirm/file")
    @ResponseBody
    public ResultBean<Object> confirmContract(@RequestHeader String accessToken,
                                      @RequestParam(value = "contractNubmer") String contractNubmer, @RequestParam(value = "filePath") String filePath) {
        return contractAppService.confirm(accessToken, contractNubmer, filePath);
    }

    @GetMapping("/setReminderTime")
    @ResponseBody
    public ResultBean<Object> setReminderTime(@RequestParam(value = "type") Integer type) {
        return contractAppService.setReminderTime(type);
    }

    /**
     * 合同详情
     *
     * @param contractNubmer
     * @return
     */
    @GetMapping("/contractDetail")
    @ResponseBody
    public ResultBean<ContractDetailVO> contractDetail(@RequestParam(value = "contractNubmer") String contractNubmer) {
        return contractAppService.contractDetail(contractNubmer);
    }
}
